﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Infrastucture;
using HubModule.ViewModels;
using TimeSplicerModule.Security;
using TimeSplicerModule.Services;
using TimeSplicerModule.DomainModels;

namespace HubModule.Services
{
    public interface IHubService
    {
        HubViewModel GetViewModel();
    }

    public class HubService : IHubService
    {
        private readonly IUnityContainer _container;
        private ISession _session;
        private IProjectService _projectService;
        private ITaskService _taskService;

        public HubService(IUnityContainer container)
        {
            _container = container;
        }

        public HubViewModel GetViewModel()
        {
            _session = _container.Resolve<ISession>();
            _taskService = _container.Resolve<ITaskService>();
            _projectService = _container.Resolve<IProjectService>();


            var model = new HubViewModel();
            model.InProgressTask = _taskService.GetInProgressTask();

            var tasks = _taskService.GetTasks(_session.User);
            var suspended = tasks.Where(x => x.Status.Status == Status.Suspended).OrderBy(o => o.LastWorked).ThenBy(o => o.DueDate).Take(3).ToList();
            var notStarted = tasks.Where(x => x.Status.Status == Status.Not_Started).OrderBy(o => o.DueDate).Take(3).ToList();


            if (model.InProgressTask != null)
                model.InProgressTaskVisibility = System.Windows.Visibility.Visible;
            else
                model.InProgressTaskVisibility = System.Windows.Visibility.Collapsed;

            
            // need to collapse all visibilities
            model.SuspendedTask1Visibillity = System.Windows.Visibility.Collapsed;
            model.SuspendedTask2Visibillity = System.Windows.Visibility.Collapsed;
            model.SuspendedTask3Visibillity = System.Windows.Visibility.Collapsed;
            model.NotStartedTask1Visibility = System.Windows.Visibility.Collapsed;
            model.NotStartedTask2Visibility = System.Windows.Visibility.Collapsed;
            model.NotStartedTask3Visibility = System.Windows.Visibility.Collapsed;


            for (int i = 0; i < suspended.Count; ++i)
            {
                if (i + 1 == 1)
                {
                    model.SuspendedTask1 = suspended[i];
                    model.SuspendedTask1Visibillity = System.Windows.Visibility.Visible;
                }
                else if (i + 1 == 2)
                {
                    model.SuspendedTask2 = suspended[i];
                    model.SuspendedTask2Visibillity = System.Windows.Visibility.Visible;
                }
                else if (i + 1 == 3)
                {
                    model.SuspendedTask3 = suspended[i];
                    model.SuspendedTask3Visibillity = System.Windows.Visibility.Visible;
                }
            }


            for (int i = 0; i < notStarted.Count; ++i)
            {
                if (i + 1 == 1)
                {
                    model.NotStartedTask1 = notStarted[i];
                    model.NotStartedTask1Visibility = System.Windows.Visibility.Visible;
                }
                else if (i + 1 == 2)
                {
                    model.NotStartedTask2 = notStarted[i];
                    model.NotStartedTask2Visibility = System.Windows.Visibility.Visible;
                }
                else if (i + 1 == 3)
                {
                    model.NotStartedTask3 = notStarted[i];
                    model.NotStartedTask3Visibility = System.Windows.Visibility.Visible;
                }
            }

            model.MyActiveProjectCount = _projectService.MyActiveProjectCount();
            model.MyActiveTaskCount = tasks.Count(x => x.Status.Status == Status.In_Progress
                || x.Status.Status == Status.Suspended
                || x.Status.Status == Status.Not_Started); //_taskService.GetMyActiveTaskCount();

            return model;
        }
    }
}
